﻿using UnityEditor;
using UnityEngine;

namespace GameToolkit.Editor
{
    [CustomEditor(typeof(StoryGraphSelection))]
    public class StoryGraphSelectionInspector : UnityEditor.Editor
    {
        SerializedProperty m_Nodes;
        SerializedProperty m_GraphAsset;

        private void OnEnable()
        {
            m_Nodes = serializedObject.FindProperty("m_GraphNodes");
            m_GraphAsset = serializedObject.FindProperty("m_GraphAsset");
            Undo.undoRedoEvent += PerformUndoRedo;
        }

        private void OnDisable()
        {
            Undo.undoRedoEvent -= PerformUndoRedo;
        }

        private void PerformUndoRedo(in UndoRedoInfo info)
        {
            if (!info.isRedo)
            {
                var inst = target as StoryGraphSelection;
                var graph = inst == null ? null : inst.currentGraph;
                if (graph != null)
                {
                    graph.IsDirty = true;
                    foreach (var sel in graph.selection)
                    {
                        if (sel is IStoryNodeVisual visual)
                            visual.Validate();
                    }
                }
            }
        }

        public override void OnInspectorGUI()
        {
            var inst = target as StoryGraphSelection;
            if (inst == null)
                return;
            serializedObject.Update();
            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.PropertyField(m_GraphAsset);
            EditorGUI.EndDisabledGroup();
            GUILayout.Space(10);
            EditorGUI.BeginChangeCheck();
            for (int i = 0; i < m_Nodes.arraySize; i++)
            {
                EditorGUILayout.BeginHorizontal("IN Title");
                var type = inst.m_GraphNodes[i].GetType();
                var info = inst.m_GraphNodes[i] == null ? null : StoryNodeLib.GetNodeInfo(type);
                var title = info == null ? "<missing>" : $"{info.Title} ({type.Name})";
                var foldout = EditorGUILayout.Foldout(inst.m_GraphNodes[i].m_Expend, title, true);
                inst.m_GraphNodes[i].m_Expend = foldout;
                EditorGUILayout.EndHorizontal();
                if (foldout && inst.m_GraphNodes[i] != null)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.PrefixLabel("GUID");
                    EditorGUILayout.SelectableLabel(inst.m_GraphNodes[i]._guid, GUILayout.Height(18));
                    EditorGUILayout.EndHorizontal();
                    var prop = m_Nodes.GetArrayElementAtIndex(i);
                    //EditorGUILayout.PropertyField(prop, true);
                    var depth = prop.depth;
                    if (prop.NextVisible(true))
                    {
                        do
                        {
                            EditorGUILayout.PropertyField(prop, true);
                        } while (prop.NextVisible(false) && prop.depth > depth);
                    }
                }
            }
            var changed = EditorGUI.EndChangeCheck();

            if (changed)
            {
                serializedObject.ApplyModifiedProperties();
                var graph = inst.currentGraph;
                if (graph != null)
                {
                    graph.IsDirty = true;
                    foreach (var sel in graph.selection)
                    {
                        if (sel is IStoryNodeVisual visual)
                            visual.Validate();
                    }
                }
            }
        }
    }
}
